#_*_ coding:utf-8 _*_
# 导入响应的模块
import pygame
# 导入所有的常量
from pygame.locals import *
import time,random


class Zidan:

    def __init__(self, x, y, screen, up):
        self.x = x + up
        self.y = y
        self.screen = screen
        self.image = pygame.image.load("images/pd.png")

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 5
        if self.y<=-20:
        	#除了边框，释放对象
        	return True
    def enemyMove(self):
        self.y+=5
        if self.y>=600:
            return True


class Enemy:
    #敌机
    zidanList=[]
    def __init__(self,screen):
        #敌机的范围，随机
        self.x = random.choice(range(0,406))
        self.y = -75
        self.screen = screen
        self.image = pygame.image.load("images/e0.png")

    def display(self):
        #子弹不连发
        if(random.choice(range(0,40))==10):
            self.fire()
        for i in self.zidanList:
            i.display()
            isOut=i.enemyMove()
            if(isOut == True):
                self.zidanList.remove(i)    
        self.screen.blit(self.image, (self.x, self.y))

    def move(self,hero):
        self.y += 4
        #遍历所以子弹对象
        for bo in hero.zidanList:
            if(bo.x>self.x+12 and bo.x <self.x+92 and bo.y>self.y+20 and bo.y<self.y+60):
                #移除子弹
                hero.zidanList.remove(bo)
                return True
        #敌机出屏幕
        if(self.y>600):
            return True
        for i in self.zidanList:
            if(i.x+1.5 > hero.x and i.x+1.5 < hero.x+106) and (i.y+17 >= hero.y):
                self.zidanList.remove(i)
                return 'heroBomb'


    def fire(self):
        self.zidanList.append(Zidan(self.x, self.y+74, self.screen, 52))





class HeroPlane:
    '''玩家飞机'''

    def __init__(self, screen_temp):
        # 定义飞机位置
        self.x = 200
        self.y = 500
        self.screen = screen_temp
        self.image = pygame.image.load("images/me.png")
        self.zidanList = []
        self.ups = [56]

    def display(self):
        # 展示子弹
        self.up()
        for i in self.zidanList:
            i.display()
            isOut=i.move()
            if(isOut == True):
            	self.zidanList.remove(i)
        self.screen.blit(self.image, (self.x, self.y))

    def move_left(self):
        self.x -= 10
        if(self.x <= 0):
            self.x = 0

    def move_right(self):
        self.x += 10
        if(self.x >= 406):
            self.x = 406
    def move_up(self):
    	self.y-=10
    	if(self.y<=0):
    		self.y=0
    def move_down(self):
    	self.y+=10
    	if(self.y>=524):
    		self.y=524


    def fire(self):
        for i in self.ups:
            self.zidanList.append(Zidan(self.x, self.y, self.screen, i))

    def up(self):
        self.ups = [0, 28, 56, 84, 112]

    def removeUp(self):
        self.ups = [56]


def key_control(hero):
    # 键盘控制函数
    # 执行退出操作
    # 获取当前event的
    for event in pygame.event.get():
        # print(event.type)
        if(event.type == QUIT):
            print('退出。。。')
            exit()
    # 获取按键信息，并执行对应的操作
    pressed_key = pygame.key.get_pressed()
    isplay =True;
    if pressed_key[K_LEFT] or pressed_key[K_a]:
        hero.move_left()
    elif pressed_key[K_RIGHT] or pressed_key[K_a]:
        hero.move_right()
    elif pressed_key[K_UP] or pressed_key[K_w]:
    	hero.move_up()
    elif pressed_key[K_DOWN] or pressed_key[K_s]:
    	hero.move_down()

    if pressed_key[K_SPACE]:
        hero.fire()
    if pressed_key[K_o]:
        # 升级
        hero.up()
    if pressed_key[K_p]:
        # 取消升级
        hero.removeUp()

class gameOver:
    def __init__(self,  screen):
        self.x = 140
        self.y = 180
        self.screen = screen
        self.image = pygame.image.load("images/gameover.png")

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 5
        if self.y<=-20:
            #除了边框，释放对象
            return True


def main():
    # 创建游戏窗口
    screen = pygame.display.set_mode((512, 600), 0, 0)
    pygame.display.set_caption("小小飞机大战")
    # 创建游戏背景
    background = pygame.image.load("images/bg2.jpg")
    # 创建hero类
    hero = HeroPlane(screen)
    #存放敌机
    enemyList=[]
    bkheight = -936
    isplay=True
    gv=gameOver(screen)
    while True:
        # 绘制画面
        screen.blit(background, (0, bkheight))
        bkheight += 2
        if bkheight >= -118:
            bkheight = -936
        key_control(hero)
        if isplay == True:
            #随机绘制敌机
            if random.choice(range(40))==10:
                emType=random.choice(range(0,3))
                ee=Enemy(screen)
                ee.image=pygame.image.load("images/e"+str(emType)+".png")
                enemyList.append(ee)
            for em in enemyList:
                em.display();
                isClear=em.move(hero);
                if(isClear == True):#子弹命中结束
                    em.x+=20
                    em.image=pygame.image.load("images/bomb3.png");
                    em.display()
                    enemyList.remove(em)
                if  (isClear == 'heroBomb') or ((em.y+72 >hero.y) and ((em.x >hero.x and em.x<hero.x+106) or(em.x+104 >hero.x and em.x+104<hero.x+106))):
                    #飞机炸掉
                    hero.image=pygame.image.load("images/bomb3.png");
                    isplay =False
                    gv.display()
                    #hero.image=pygame.image.load("images/me.png");
        else:
            bomType=random.choice(range(0,4))
            hero.image=pygame.image.load("images/bomb"+str(bomType)+".png");
            gv.image=pygame.image.load("images/gameover.png")
            gv.display()
        hero.display()
        

        # 更新显示
        pygame.display.update()

        # 定时显示
        time.sleep(0.04)


# 判断当前是否为主运行程序
if __name__ == '__main__':
    main()
